package com.xspace.android.xafcommon.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xspace.android.xafcommon.R;
import com.xspace.android.xafcommon.util.LogUtil;

import java.util.Arrays;
import java.util.List;


/**
 * 条目选择对话框
 * <p>
 * Created by liuchen on 2016/11/3.
 */
public class SelectDialog extends Dialog {

    private LinearLayout linearLayout;
    private List<String> mList;
    private ISelectItemListener mListener;
    private Context mContext;


    private SelectDialog(Context context, int theme, String strMessage) {
        super(context, theme);
        mContext = context;
        this.setContentView(R.layout.dialog_select);
        this.setCancelable(true);
        findView();
        LogUtil.d("tvMsg>>" + strMessage);
    }

    private void findView() {
        linearLayout = (LinearLayout) findViewById(R.id.linear_select);
    }

    private void bindView() {
        int index = 0;
        for (final String arg : mList) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_select_item, linearLayout, false);
            TextView txt = ((TextView) view.findViewById(R.id.txt_show));
            txt.setText(arg);
            txt.setTag(index);
            txt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (v.getTag() instanceof Integer) {
                        mListener.selectItemClick((Integer) v.getTag());
                        dismiss();
                    }
                }
            });
            linearLayout.addView(view);
            index++;
        }
    }

    @Override
    public void show() {
        super.show();
    }

    public static class Builder {
        SelectDialog selectDialog;

        public Builder(Context context, int theme) {
            selectDialog = new SelectDialog(context, theme, "");
        }

        public Builder(Context context) {
            this(context, R.style.CustomDialog);
        }

        public void setSingleChoiceItems(String[] arrays, ISelectItemListener listener) {
            List<String> list = Arrays.asList(arrays);
            setSingleChoiceItems(list, listener);
        }

        public void setSingleChoiceItems(List<String> list, ISelectItemListener listener) {
            selectDialog.mList = list;
            selectDialog.mListener = listener;
            selectDialog.bindView();
        }

        public void show() {
            selectDialog.show();
        }

        public void dismiss() {
            selectDialog.dismiss();
        }
    }

    public interface ISelectItemListener {
        void selectItemClick(int position);
    }


}
